package org.artifact.core.lang;

import cn.hutool.core.util.ArrayUtil;

import java.util.Random;

/**
 * @author : SandKing
 * @CreateDate : 2019/5/29 10:33
 * @Description ：Please describe this document
 */
public class ShortId {
    private int min;
    private int max;

    private int[] ids;

    private int pos;

    public ShortId(int min, int max, int[] filter) {
        if (filter == null) {
            filter = new int[0];
        }
        ids = new int[max - min - filter.length];
        int index = 0;
        for (int i = min; i < max; i++) {
            if (ArrayUtil.contains(filter, i)) {
                continue;
            }
            ids[index++] = i;
        }

        shuffle(ids);

    }

    private void shuffle(int[] arr) {
        Random rnd = new Random();
        for (int i = arr.length; i > 1; i--) {
            swap(arr, i - 1, rnd.nextInt(i));
        }
    }

    private void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public int next() {
        return ids[pos++];
    }
}
